package com.night.service;

import com.alibaba.fastjson.JSONObject;
import com.night.util.MD5Util;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.MessageDigest;
import java.util.*;

public class HttpUtil {

    private static Logger logger = LoggerFactory.getLogger(HttpUtil.class);

    private static final char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

    public static void main(String[] args) throws Exception{
        get();
    }

    //模拟get请求
    public static void get() throws Exception{
        String companyName="百望股份有限公司";
        String platformNo="VTM";
        String time=String.valueOf(System.currentTimeMillis());
        String url="http://192.168.31.232:8078/sys/invoice/companySearch";
        String signKey="123456";
        url=url+"?platformNo="+platformNo+"&companyName="+companyName;

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("companyName",companyName);
        jsonObject.put("platformNo",platformNo);



        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet request =new HttpGet(url);
        request.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) ...");
        request.setHeader("systemCode","1");
        request.setHeader("time",time);
        request.setHeader("signKey",signKey);
        request.setHeader("needSign","true");


        //计算签名
        List<String> signList = new ArrayList<String>();
        signList.add("companyName");
        signList.add("platformNo");
        Collections.sort(signList);

        StringBuilder sb=new StringBuilder();
        sb.append(time);
        for (String paramsName:signList){
            sb.append("&").append(paramsName).append("=").append(jsonObject.getString(paramsName));
        }
        sb.append("&");
        sb.append(signKey);
        System.out.println("请求串："+sb.toString());

        String sign= MD5Util.md5(sb.toString());
        request.setHeader("sign",sign);//签名
        CloseableHttpResponse response = httpclient.execute(request);
        int statusCode=response.getStatusLine().getStatusCode();
        if(statusCode==200){
            String responseBody = EntityUtils.toString(response.getEntity());
            JSONObject json=JSONObject.parseObject(responseBody);
            String data=json.getString("data");
            String encryptionType=json.getString("encryptionType");
            String secretKeyText=json.getString("secretKeyText");
            System.out.println("加密方式："+encryptionType);
            System.out.println("加密密钥："+secretKeyText);
            System.out.println("加密密文："+data);
        }
    }

    public static void post() throws Exception{

    }

}
